package com.sky.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.entity.User;
import com.sky.mapper.admin.OrdersMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.admin.WorkspaceMapper;
import com.sky.mapper.user.UserMapper;
import com.sky.service.admin.DishService;
import com.sky.service.admin.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
@RequiredArgsConstructor
public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceMapper, BusinessDataVO> implements WorkspaceService{
    private final CategoryServiceImpl categoryService;
    private final DishService dishService;
    private final OrdersMapper ordersMapper;
    private final UserMapper userMapper;
    private final SetmealMapper setmealMapper;

    /**
     * 获取菜品总览数据
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        QueryWrapper<Dish> status1 = new QueryWrapper<Dish>().eq("status", 1);
        QueryWrapper<Dish> status0 = new QueryWrapper<Dish>().eq("status", 0);
        int sold = dishService.list(status1).size();
        int discontinued = dishService.list(status0).size();
        return DishOverViewVO.builder().sold(sold).discontinued(discontinued).build();
    }



    /**
     * 获取营业数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData() {

        Double turnover = ordersMapper.getTurnover(LocalDateTime.now());
        if (turnover == null){
            turnover = 0.0;
        }

        Integer validOrderCount = Math.toIntExact(ordersMapper.selectCount(new QueryWrapper<Orders>()
                .eq("status", Orders.COMPLETED)
                .ge("order_time", LocalDateTime.now().with(LocalTime.MIN))
                .le("order_time", LocalDateTime.now().with(LocalTime.MAX))));

        double orderCompletionRate = 0.0;
        double unitPrice = 0.0;
        try {
            int totalOrderCount = Math.toIntExact(ordersMapper.selectCount(new QueryWrapper<Orders>()
                    .ge("order_time", LocalDateTime.now().with(LocalTime.MIN))
                    .le("order_time", LocalDateTime.now().with(LocalTime.MAX))));
            if (totalOrderCount != 0)
                orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            if (validOrderCount != 0)
                unitPrice = turnover / validOrderCount;
        }catch (Exception e){
            throw new RuntimeException("今日没有完成订单");
        }


        Integer newUsers = Math.toIntExact(userMapper.selectCount(new QueryWrapper<User>()
                .ge("create_time", LocalDateTime.now().with(LocalTime.MIN))
                .le("create_time", LocalDateTime.now().with(LocalTime.MAX))));

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Integer discontinued = Math.toIntExact(setmealMapper.selectCount(new QueryWrapper<Setmeal>().eq("status", 0)));
        Integer sold = Math.toIntExact(setmealMapper.selectCount(new QueryWrapper<Setmeal>().eq("status", 1)));
        return SetmealOverViewVO.builder()
                        .discontinued(discontinued)
                        .sold(sold)
                        .build();
    }


    /**
     * 获取订单总览数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        Integer allOrders = Math.toIntExact(ordersMapper.selectCount(new QueryWrapper<Orders>()));
        Integer cancelledOrders = Math.toIntExact(ordersMapper.selectCount(new QueryWrapper<Orders>().eq("status", Orders.CANCELLED)));
        Integer completedOrders = Math.toIntExact(ordersMapper.selectCount(new QueryWrapper<Orders>().eq("status", Orders.COMPLETED)));
        Integer deliveredOrders = Math.toIntExact(ordersMapper.selectCount(new QueryWrapper<Orders>().eq("status", Orders.DELIVERY_IN_PROGRESS)));
        Integer waitingOrders = Math.toIntExact(ordersMapper.selectCount(new QueryWrapper<Orders>().eq("status", Orders.TO_BE_CONFIRMED)));
        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }


}
